<?php

namespace app\common\service;

use app\common\service\BalanceService;

use think\Db;

/**
 * 交易
 */
class TradeService{

	/*
	挂卖
	 */
	public static function setSellOrder($uid,$cid,$amount,$price,$rate,$fee){
		Db::startTrans();
		$res1 = BalanceService::updateBalance($uid,$cid,-$amount,'交易挂卖',61);
		if($fee != 0){
			$res2 = BalanceService::updateBalance($uid,$cid,-$fee,'交易挂卖手续费',62);
		} else {
			$res2 = 1;
		}
		//添加记录
		$res3 = model('TradeListModel')->addData($uid,$cid,1,$amount,$price,$rate,$fee);
		if($res1 && $res2 && $res3){
			Db::commit();
			return true;
		} else {
			Db::rollback();
			return false;
		}
	}
	/*
	撤销卖单
	 */
	public static function undoSell($data){
		Db::startTrans();
		$res1 = BalanceService::updateBalance($data['uid'],$data['cid'],$data['left_amount'],'撤销挂卖退回',66);
		if($data['rate'] != 0){
			$fee = $data['left_amount'] * $data['rate'];
			$res2 = BalanceService::updateBalance($data['uid'],$data['cid'],$fee,'撤销挂卖退回手续费（按比例退回）',67);
		} else {
			$res2 = 1;
		}
		//添加记录
		$res3 = model('TradeListModel')->updateStatus($data['id'],2);
		if($res1 && $res2 && $res3){
			Db::commit();
			return true;
		} else {
			Db::rollback();
			return false;
		}
	}

	/*
	挂买
	 */
	public static function setBuyOrder($uid,$cid,$amount,$price){
		//添加记录
		$res = model('TradeListModel')->addData($uid,$cid,0,$amount,$price,0,0,0,0);
		if($res){
			return true;
		} else {
			return false;
		}
	}
	/*
	撤销挂买
	 */
	public static function undoBuy($id){
		$res = model('TradeListModel')->updateStatus($id,2);
		if($res){
			return true;
		} else {
			return false;
		}
	}


	/*
	买入
	 */
	public static function buy($uid,$sell_uid,$order_id,$cid,$amount,$price){
		Db::startTrans();
		$res1 = model('TradeRecordModel')->addData($uid,$sell_uid,$order_id,$cid,$amount,$price,0,0);
		$res2 = model('TradeListModel')->updateRecord($order_id,$amount);
		if($res1 && $res2){
			Db::commit();
			return true;
		} else {
			Db::rollback();
			return false;
		}
	}


	/*
	卖出
	 */
	public static function sell($uid,$buy_uid,$order_id,$cid,$amount,$price,$fee){
		Db::startTrans();
		$res1 = BalanceService::updateBalance($uid,$cid,-$amount,'交易卖出',64);
		if($fee != 0){
			$res2 = BalanceService::updateBalance($uid,$cid,-$fee,'交易卖出手续费',65);
		} else {
			$res2 = 1;
		}
		$res3 = model('TradeRecordModel')->addData($buy_uid,$uid,$order_id,$cid,$amount,$price,$fee);
		$res4 = model('TradeListModel')->updateRecord($order_id,$amount);
		if($res1 && $res2 && $res3 && $res4){
			Db::commit();
			return true;
		} else {
			Db::rollback();
			return false;
		}
	}

	/*
	当前委托
	 */
	public static function Tradeing($uid){
		$data = model('TradeRecordModel')->Tradeing($uid);
		foreach ($data as $key => $val) {
			if($val['sell_uid'] == $uid){
				$data[$key]['type'] = 1;
				$data[$key]['opposite_username'] = model('UserModel')->getUserInfo($val['buy_uid'])['username'];
				switch ($val['status']) {
					case '1':
						$data[$key]['remark'] = '等待买家打款';
						break;
					case '2':
						$data[$key]['remark'] = '确认订单';
						break;
					default:
						# code...
						break;
				}
			} else {
				$data[$key]['type'] = 0;
				$data[$key]['opposite_username'] = model('UserModel')->getUserInfo($val['sell_uid'])['username'];
				switch ($val['status']) {
					case '1':
						$data[$key]['remark'] = '标记付款';
						break;
					case '2':
						$data[$key]['remark'] = '等待卖家确认收款';
						break;
					default:
						# code...
						break;
				}
			}
		}
		return $data;
	}

	/*
	订单详情
	 */
	public static function getTradeDetail($uid,$id){
		$info = model('TradeRecordModel')->getTradeInfo($id);
		if($info['buy_uid'] == $uid){
			$opposite_uid = $info['sell_uid'];
			$info['type'] = 0;
			$info['type_name'] = '买方';

		} elseif($info['sell_uid'] == $uid){
			$opposite_uid = $info['buy_uid'];
			$info['type'] = 1;
			$info['type_name'] = '卖方';
		} 
		$opposite_info    = model('UserModel')->getUserInfo($opposite_uid);
		$pay_info         = model('PayInfoModel')->getPayInfo($info['sell_uid']);
		$data['order_info'] = $info;
		$data['pay_info']   = $pay_info;
		$data['opposite']   = [];
		$data['opposite']['username'] = $opposite_info['username'];
		$data['opposite']['phone'] = $opposite_info['phone'];
		return $data;
 	}


	/*
	已完成订单
	 */
	public static function completeTrade($uid){
		$data = model('TradeRecordModel')->completeTrade($uid);
		foreach ($data as $key => $val) {
			if($val['sell_uid'] == $uid){
				$data[$key]['type'] = 1;
				$data[$key]['opposite_username'] = model('UserModel')->getUserInfo($val['buy_uid'])['username'];
				switch ($val['status']) {
					case '3':
						$data[$key]['remark'] = '交易完成';
						break;
					case '4':
						$data[$key]['remark'] = '买家取消订单';
						break;
					case '5':
						$data[$key]['remark'] = '已拒绝收款';
						break;
					default:
						# code...
						break;
				}
			} else {
				$data[$key]['type'] = 0;
				$data[$key]['opposite_username'] = model('UserModel')->getUserInfo($val['sell_uid'])['username'];
				switch ($val['status']) {
					case '3':
						$data[$key]['remark'] = '交易完成';
						break;
					case '4':
						$data[$key]['remark'] = '已取消';
						break;
					case '5':
						$data[$key]['remark'] = '卖家拒绝收款';
						break;
					default:
						# code...
						break;
				}
			}
		}
		return $data;
	}

	/*
	标记付款
	 */
	public static function confirmPay($id,$pay_cert,$remark){
		$res  = model('TradeRecordModel')->confirmPay($id,$pay_cert,$remark);
		return $res;
	}	
	
	// /*
	// 确认交易
	//  */
	// public static function confirmTrade($id){
	// 	$data = model('TradeRecordModel')->getTradeInfo($id);
	// 	$uid = $data['buy_uid'];
	// 	$amount = $data['amount'];
	// 	$cid = $data['cid'];
	// 	//打给买家
	// 	Db::startTrans();
	// 	$res1 = BalanceService::updateBalance($uid,$cid,$amount,'交易买入',83);
	// 	$res2 = model('TradeRecordModel')->confirmTrade($id);
	// 	if($res1 && $res2){
	// 		Db::commit();
	// 		return true;
	// 	} else {
	// 		Db::rollback();
	// 		return false;
	// 	}
	// }
	/*
	确认交易
	 */
	public static function confirmTrade($id){
		$data = model('TradeRecordModel')->getTradeInfo($id);
		$uid = $data['buy_uid'];
		$amount = $data['amount'];
		$cid = $data['cid'];
		//打给买家
		Db::startTrans();
		$res1 = BalanceService::updateBalance($uid,$cid,$amount,'交易买入',63);
		$res2 = model('TradeRecordModel')->confirmTrade($id);
		if($res1 && $res2){
			Db::commit();
			return true;
		} else {
			Db::rollback();
			return false;
		}
	}
	/*
	拒绝交易
	 */
	public static function refuseTrade($id,$refuse_remark){
		$res = model('TradeRecordModel')->refuseTrade($id,$refuse_remark);
		if($res){
			return true;
		} else {
			return false;
		}
	}
}